﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PublishSoftwareWebPageCreator
{
    public partial class CutImageForm : Form
    {
        Image resourceImage;
        public string picFileName;
        public string resourceImageFileName;
        Page page;

        public string takePicFilePath()
        {
            return this.page.takeDir() + "\\asset\\" + this.picFileName;
        }

        public CutImageForm(Page _page)
        {
            this.page = _page;
            InitializeComponent();
            this.pic.Parent = this.picPanel;
            this.pic.SizeMode = PictureBoxSizeMode.AutoSize;
            this.pic.Dock = DockStyle.None;
            this.pic.Location = new Point(0, 0);
            this.pic.Size = this.picPanel.Size;

            this.pic.SendToBack();
            this.cutBox.BackColor = Color.Transparent;//将Panel设为透明
            this.cutBox.Parent = this.pic;//将panel父控件设为背景图片控件
            this.cutBox.BringToFront();//将panel放在前面
            
            ((Control)cutBox).AllowDrop = true;
        }

        private void bt_open_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                resourceImageFileName = ofd.FileName;
                resourceImage = Image.FromFile(ofd.FileName);
                resizeScrollBar.Value = 1000;
                pic.Image = resourceImage;
                lb_msg.Text = "已打开图片";
            }
        }

        private void bt_cut_Click(object sender, EventArgs e)
        {
            if (cutBox.Visible)
            {
                Rectangle r = new Rectangle(cutBox.Location, cutBox.Size);
                Bitmap bmp;
                bmp = WaySOne(r);
                cutBox.Image = bmp;
                lb_msg.Text = "已裁剪";
            }
        }

        Bitmap WaySOne(Rectangle r)
        {
            Bitmap b = new Bitmap(r.Width, r.Height);

            r.X += 1;
            r.Y += 1;
            r.Width -= 2;
            r.Height -= 2;
            var i = ((Bitmap)pic.Image).Clone(r, PixelFormat.Format32bppArgb);

            r.X = 0;
            r.Y = 0;

            using (Graphics g = Graphics.FromImage(b))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                using (System.Drawing.Drawing2D.GraphicsPath p = new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Alternate))
                {
                    p.AddEllipse(r);
                    g.FillPath(new TextureBrush(i), p);
                }
                //g.FillEllipse(new TextureBrush(i), 0, 0, i.Width, i.Height);
            }
            b.Save("y.png");
            return b;
        }

        private void bt_user_pic_Click(object sender, EventArgs e)
        {
            cutBox.Visible = true;
            cutBox.Size = new Size(60, 60);
            lb_msg.Text = "设置裁剪60*60圆形图片";
        }

        private void bt_dev_pic_Click(object sender, EventArgs e)
        {
            cutBox.Visible = true;
            cutBox.Size = new Size(150, 150);
            lb_msg.Text = "设置裁剪150*150圆形图片";
        }

        private void bt_software_pic_Click(object sender, EventArgs e)
        {
            cutBox.Visible = false;
            lb_msg.Text = "保持原图不裁剪";
        }

        private void CutImageForm_Load(object sender, EventArgs e)
        {
            tb_pic_filename.Text = DateTime.Now.Ticks.ToString() + ".png";
        }

        int xPos;
        int yPos;
        bool MoveFlag;
        private void cutBox_MouseDown(object sender, MouseEventArgs e)
        {
            MoveFlag = true;//已经按下.
            xPos = e.X;//当前x坐标.
            yPos = e.Y;//当前y坐标.
        }

        private void cutBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (MoveFlag)
            {
                cutBox.Left += Convert.ToInt16(e.X - xPos);//设置x坐标.
                cutBox.Top += Convert.ToInt16(e.Y - yPos);//设置y坐标.
            }
        }

        private void cutBox_MouseUp(object sender, MouseEventArgs e)
        {
            MoveFlag = false;
        }

        private void resizeScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            int v = e.NewValue;
            reducedImage(v);
        }

        private void reducedImage(int v)
        {
            if (resourceImage == null)
                return;

            int width = resourceImage.Width * v / 1000;
            int height = resourceImage.Height * v / 1000;
            try
            {
                Image data = null;
                //用指定的大小和格式初始化Bitmap类的新实例 
                using (Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                {
                    //从指定的Image对象创建新Graphics对象 
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        //清除整个绘图面并以透明背景色填充 
                        //graphics.Clear(Color.Transparent);
                        //在指定位置并且按指定大小绘制原图片对象 
                        graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height));
                    }
                    pic.Image = new Bitmap(bitmap);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private void bt_ok_Click(object sender, EventArgs e)
        {
            if (!cutBox.Visible)
            {
                this.DialogResult = DialogResult.OK;
                this.picFileName = tb_pic_filename.Text.Trim();
                File.Copy(resourceImageFileName, this.page.takeDir() + "\\asset\\" + this.picFileName);
                this.Close();
            }
            else
            { 
                this.DialogResult = DialogResult.OK;
                this.picFileName = tb_pic_filename.Text.Trim();
                cutBox.Image.Save(this.page.takeDir() + "\\asset\\" + this.picFileName);
                this.Close();
            }
        }
    }
}
